<?php
// +----------------------------------------------------------------------
// | yylAdmin 前后分离，简单轻量，免费开源，开箱即用，极简后台管理系统
// +----------------------------------------------------------------------
// | Copyright https://gitee.com/skyselang All rights reserved
// +----------------------------------------------------------------------
// | Gitee: https://gitee.com/skyselang/yylAdmin
// +----------------------------------------------------------------------

namespace app\api\controller\magpie;

use app\common\controller\BaseController;
use app\common\service\magpie\MagpieMemberService;
use app\common\validate\magpie\MagpieMemberValidate;
use hg\apidoc\annotation as Apidoc;

use app\common\service\magpie\MagpieService;
use app\common\validate\magpie\MagpieValidate;

use app\common\service\utils\QrcodeUtils;

/**
 * @Apidoc\Title("抽奖")
 * @Apidoc\Group("magpie")
 * @Apidoc\Sort("250")
 */
class MagpieMember extends BaseController
{
    /**
    * @Apidoc\Title("抽奖列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="抽奖列表", children={
    *   @Apidoc\Returned(ref="app\common\model\MagpieMemberModel", field="id,title,content,is_disable,create_time,update_time")
    * })
    */
    public function list()
    {
        $where = $this->where(where_delete());

        $data = MagpieMemberService::list($where, $this->page(), $this->limit(), $this->order());

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖信息")
    * @Apidoc\Query(ref="app\common\model\MagpieMemberModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\MagpieMemberModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);

        validate(MagpieMemberValidate::class)->scene('info')->check($param);

        $data = MagpieMemberService::info($param['id']);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\MagpieMemberModel", field="title,content,is_disable")
    */
    public function add()
    {
        $param = $this->params(MagpieMemberService::$edit_field);
        
        unset($param['id']);

        validate(MagpieMemberValidate::class)->scene('add')->check($param);

        $data = MagpieMemberService::add($param);
        
        $data['qrcode'] = QrcodeUtils::mkCode($data['sn']);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖修改")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\MagpieMemberModel", field="id,title,content,is_disable")
    */
    public function edit()
    {
        $param = $this->params(MagpieMemberService::$edit_field);

        validate(MagpieMemberValidate::class)->scene('edit')->check($param);

        $data = MagpieMemberService::edit($param['id'], $param);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);

        validate(MagpieMemberValidate::class)->scene('dele')->check($param);

        $data = MagpieMemberService::dele($param['ids']);

        return success($data);
    }

    /**
     * @Apidoc\Title("抽奖禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\MagpieMemberModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);

        validate(MagpieMemberValidate::class)->scene('disable')->check($param);

        $data = MagpieMemberService::edit($param['ids'], $param);

        return success($data);
    }
    
    public function MagpieInfo(){
        $param = $this->params(['id/d' => '']);

        validate(MagpieValidate::class)->scene('info')->check($param);

        $data = MagpieService::info($param['id']);

        return success($data);
    }
}
